package com.okhttpdemo.http;

import android.app.ProgressDialog;
import android.content.Context;

import java.util.concurrent.Executor;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by 黄海 on 2017/2/28.
 */

public class HttpUtils {
    private static OkHttpClient okHttpClient;

    public static Executor getUiExecutor() {
        return uiExecutor;
    }

    private static Executor uiExecutor;

    public static void init(OkHttpClient client, Executor executor) {
        if (client == null) throw new NullPointerException("client can't be  null");
        if (executor == null) throw new NullPointerException("executor can't be  null");
        if (okHttpClient == null) {
            synchronized (HttpUtils.class) {
                if (okHttpClient == null) {
                    okHttpClient = client;
                }
            }
        }
        uiExecutor = executor;
    }

    public static OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public static ProgressDialog showProgressDialog(Context context) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();
        return progressDialog;
    }

    public static MyCall newCall(Request request) {
        Call call = okHttpClient.newCall(request);
        return new MyCall(call, uiExecutor);
    }

}
